package com.sjgl.esdn.management.controller;

import com.sjgl.esdn.management.entity.AjaxResult;
import com.sjgl.esdn.management.entity.FileBrowse;
import com.sjgl.esdn.management.entity.FileDownload;
import com.sjgl.esdn.management.entity.FileManagement;
import com.sjgl.esdn.management.service.FileManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/fileLog")
public class FileManagementController {
    @Autowired
    private FileManagementService FileManagementService;

    @RequestMapping("/fileId/{fileId}/{offset}/{limit}")
    @ResponseBody
    private AjaxResult getByFileId(@PathVariable("fileId") Long fileId, @PathVariable("offset")int offset, @PathVariable("limit") int limit){
        List<FileManagement> list = FileManagementService.findFileId(fileId,offset,limit);
        return AjaxResult.success(list);
    }

    @RequestMapping("/search/{keyWord}/{offset}/{limit}")
    @ResponseBody
    private AjaxResult fileSearch(@PathVariable("keyWord") String keyWord, @PathVariable("offset")int offset, @PathVariable("limit") int limit){
        List<FileManagement> list = FileManagementService.fileSearch("%"+keyWord+"%",offset,limit);
        return AjaxResult.success(list);
    }

    @RequestMapping("/browse/{keyWord}/{fileId}/{offset}/{limit}")
    @ResponseBody
    private AjaxResult browseSearch(@PathVariable("keyWord") String keyWord,@PathVariable("fileId")Long fileId, @PathVariable("offset")int offset, @PathVariable("limit") int limit){
        List<FileBrowse> list = FileManagementService.fileBrowse("%"+keyWord+"%",fileId,offset,limit);
        return AjaxResult.success(list);
    }

    @RequestMapping("/download/{keyWord}/{fileId}/{offset}/{limit}")
    @ResponseBody
    private AjaxResult downloadSearch(@PathVariable("keyWord") String keyWord,@PathVariable("fileId")Long fileId, @PathVariable("offset")int offset, @PathVariable("limit") int limit){
        List<FileDownload> list = FileManagementService.fileDownload("%"+keyWord+"%",fileId,offset,limit);
        return AjaxResult.success(list);
    }

    @RequestMapping("/fileOff/{fileId}/{abandonReason}/{abandonTime2}/{abandonId}")
    @ResponseBody
    private int updateType(@PathVariable("fileId") Long fileId, @PathVariable("abandonReason") String abandonReason,
                          @PathVariable("abandonTime2") String abandonTime2, @PathVariable("abandonId") Long abandonId) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date abandonTime = sdf.parse(abandonTime2);

        int result = FileManagementService.updateFileType(fileId, abandonReason,abandonTime, abandonId);
        return result;
    }
    /**
     * 返回成功
     */
    public AjaxResult success()
    {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error()
    {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message)
    {
        return AjaxResult.success(message);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message)
    {
        return AjaxResult.error(message);
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result)
    {
        return result ? success() : error();
    }
}
